package com.qidian.mes.dv.service.Impl;

import com.qidian.mes.dv.mapper.SubjectMapper;
import com.qidian.mes.dv.service.SubjectService;
import com.qidian.mes.pojo.Subject;
import com.qidian.mes.util.dot.PagerDTO;
import com.qidian.mes.util.pojo.QdResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author 陆雯城
 * @Description
 * @Time 2024-04-09 15:25
 */
@Service
public class SubjectServiceImpl implements SubjectService {
    @Autowired
    private SubjectMapper subjectMapper;

    @Override
    public QdResult deleteBySubjectIds(Long[] subjectIds) {
        subjectMapper.deleteBySubjectIds(subjectIds);
        return QdResult.success();
    }

    @Override
    public QdResult deleteBySubjectId(Long subjectId) {
        subjectMapper.deleteBySubjectId(subjectId);
        return QdResult.success();
    }

    @Override
    public QdResult updateSubject(Subject subject) {
        if (subject.getSubjectId() == null){
            return QdResult.error("SubjectId is null");
        }
        Subject tempSubject = subjectMapper.selectBySubjectCode(subject.getSubjectCode());
        if (tempSubject != null && !tempSubject.getSubjectId().equals(subject.getSubjectId())){
            return QdResult.duplicate("duplicate: subjectCode");
        }
        tempSubject = subjectMapper.selectBySubjectName(subject.getSubjectName());
        if (tempSubject != null && !tempSubject.getSubjectId().equals(subject.getSubjectId())){
            return QdResult.duplicate("duplicate: subjectName");
        }
        return QdResult.isSuccess(subjectMapper.updateSubject(subject));
    }

    @Override
    public QdResult queryBySubjectId(Long subjectId) {
        return QdResult.success(subjectMapper.selectBySubjectId(subjectId));
    }

    @Override
    public QdResult saveSubject(Subject subject) {
        if (subjectMapper.selectBySubjectName(subject.getSubjectName()) != null){
            return QdResult.duplicate("duplicate: subjectName");
        }
        if (subjectMapper.selectBySubjectCode(subject.getSubjectCode()) != null){
            return QdResult.duplicate("duplicate: subjectCode");
        }
        return QdResult.isSuccess(subjectMapper.insertSubject(subject));
    }

    @Override
    public QdResult<PagerDTO<Subject>> queryByPage(Subject subject, int page, int limit) {
        PagerDTO<Subject> pagerDTO = new PagerDTO<>();
        pagerDTO.setPage(page);
        pagerDTO.setLimit(limit);
        pagerDTO.setTotal(subjectMapper.selectCount(subject));
        pagerDTO.setData(subjectMapper.selectByPage(subject,(page-1)*limit,limit));
        return QdResult.success(pagerDTO);
    }
}
